home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1995 March / Macworld CD-ROM (March 1995).cdr / Updaters / AppMaker 1.5.2->1.5.4 / Libraries / THINK / AMLibraryC / WindowAids.c < prev   
Encoding:
C/C++ Source or Header  |  1993-05-19  |  10.6 KB  |  493 lines  |  [TEXT/KAHL]

  1. /* © 1988-91, Bowers Development Corp. */
  2. /* WindowAids.c */
  3.  
  4. #include <Types.h>
  5. #include <Quickdraw.h>
  6. #include <Controls.h>
  7. #include <Dialogs.h>
  8. #include <Events.h>
  9. #include <Fonts.h>
  10. #include <Lists.h>
  11. #include <Menus.h>
  12. #include <Resources.h>
  13. #include <ToolUtils.h>
  14.  
  15. #include "Globals.h"
  16.  
  17. #include "WindowAids.h"
  18.  
  19. short        textJust;        /* set by SetWFont */
  20.  
  21. #define topLeft(r)    (((Point *) &(r))[0])
  22.  
  23. #pragma segment WindowAids
  24.     
  25. typedef struct {
  26.         Handle            reserved;
  27.         Rect            displayRect;
  28.         Byte            itemKind;
  29.         Byte            dataLen;
  30. } ItemTemplate, *ItemTPtr;
  31.  
  32. #define ItemTBaseSize    sizeof (ItemTemplate)
  33.  
  34. typedef struct {
  35.         short            maxItem;
  36.         ItemTemplate    items;    /* repeated */
  37. } WitlTemplate, *WitlTPtr, **WitlTHndl;
  38.  
  39. /*----------*/
  40. void    GetWRect        (short            itemNr,
  41.                          Rect            *itemRect)
  42. {
  43.     WitlTHndl        witlHandle;
  44.     ItemTPtr        wItemPtr;
  45.     short            I;
  46.     short            dataSize;
  47.  
  48.     SetRect (itemRect, 0, 0, 16, 16);
  49.     witlHandle = (WitlTHndl) cur->witlHandle;
  50.     if ((witlHandle != NULL)
  51.     &&  (itemNr <= (**witlHandle).maxItem + 1) ) {
  52.         wItemPtr = &(**witlHandle).items;
  53.         for (I = 1; I <= itemNr - 1; I++) {
  54.             dataSize = wItemPtr->dataLen + (wItemPtr->dataLen & 1);
  55.             wItemPtr = (ItemTPtr) (((Ptr)wItemPtr) + (ItemTBaseSize + dataSize));
  56.         } /*for*/
  57.         *itemRect = wItemPtr->displayRect;
  58.     }
  59. } /* GetWRect */
  60.     
  61. /*----------*/
  62. typedef struct {
  63.         short            header;
  64.         short            offset;
  65. } ictbEntry, *ictbEntryPtr;
  66.  
  67. typedef struct {
  68.         ictbEntry        item [5000];
  69. } WictTemplate, *WictTPtr, **WictTHndl;
  70.  
  71. typedef struct {
  72.         short            fontNum;
  73.         Style            typeStyle;
  74.         SignedByte        just;        /* AppMaker adition */
  75.         short            typeSize;
  76.         RGBColor        fgColor;
  77.         RGBColor        bgColor;
  78.         short            xferMode;
  79. } StyleData, *StyleDataPtr;
  80.  
  81. /* defined in TextEdit: */
  82. /*    doFont            = 1;        set font (family) number */
  83. /*    doFace            = 2;        set character style */
  84. /*    doSize            = 4;        set type size */
  85. /*    doColor            = 8;        set color */
  86. /*    addSize            = 16;        adjust type size */
  87.  
  88. /* set background color */
  89. #define doBgColor    0x2000
  90.  
  91. /* set transfer mode */
  92. #define doXferMode    0x4000
  93.  
  94. /* set font by name */
  95. #define doByName    0x8000
  96.  
  97. /*----------*/
  98. void    SetWFont        (short            itemNr)
  99. {
  100.     WictTHndl        wictHandle;
  101.     Ptr                wictPtr;
  102.     ictbEntryPtr    itemPtr;
  103.     short            header;
  104.     short            offset;
  105.     StyleDataPtr    dataPtr;
  106.     StyleData        data;
  107.     StringPtr        fontPtr;
  108.     Str255            fontName;
  109.  
  110.     wictHandle = (WictTHndl) cur->wictHandle;
  111.     if (wictHandle != nil) {
  112.         wictPtr = (Ptr) &(**wictHandle);
  113.         itemPtr = &(**wictHandle).item [itemNr - 1];
  114.         header = itemPtr->header;
  115.         offset = itemPtr->offset;
  116.         dataPtr = (StyleDataPtr) (wictPtr + offset);
  117.         data = *dataPtr;
  118.         if ((header & doFont) == 0) {
  119.             data.fontNum = 0;
  120.         } else {
  121.             if ((header & doByName) != 0) {
  122.                 fontPtr = (StringPtr) (wictPtr + data.fontNum);
  123.                 BlockMove (fontPtr, &fontName, 256);
  124.                 GetFNum (fontName, &data.fontNum);
  125.             }
  126.         }
  127.         if ((header & doFace) == 0) {
  128.             data.typeStyle = 0;
  129.         }
  130.         if ((header & doSize) == 0) {
  131.             data.typeSize = 0;
  132.         }
  133.         if ((header & doXferMode) == 0) {
  134.             data.xferMode = srcCopy;
  135.         }
  136.         TextFont (data.fontNum);
  137.         TextFace (data.typeStyle);
  138.         TextMode (data.xferMode);
  139.         TextSize (data.typeSize);
  140.         textJust = data.just;
  141.     }
  142. } /* SetWFont */
  143.     
  144. /*----------*/
  145. WindowPtr GetWindow        (short            windowID)
  146. {
  147.     if (sysConfig.hasColorQD) {
  148.         return (GetNewCWindow (windowID, NULL, (WindowPtr) -1L));
  149.     } else {
  150.         return (GetNewWindow (windowID, NULL, (WindowPtr) -1L));
  151.     }
  152. } /* GetWindow */
  153.     
  154. /*----------*/
  155. ListHandle NewV1List    (Rect            bounds,
  156.                           WindowPtr        parentWindow)
  157. {
  158.     Rect            listBounds;
  159.     Rect            dataBounds;
  160.     Point            cSize;
  161.     ListHandle        list;
  162.  
  163.     listBounds = bounds;
  164.     listBounds.right = listBounds.right - 15;    /*for scroll bar*/
  165.     SetRect (&dataBounds, 0, 0, 1, 0);    /*one column, no rows*/
  166.     SetPt (&cSize, listBounds.right - listBounds.left, 0);
  167.     list = LNew    (&listBounds,    /*dialog item's box*/
  168.                  &dataBounds,    /*one column, no rows*/
  169.                  cSize,            /*cell size: full width, standard height*/
  170.                  0,                /*procid - standard text list*/
  171.                  parentWindow,    /*parent window*/
  172.                  false,            /*draw it*/
  173.                  false,            /*has no grow*/
  174.                  false,            /*no horizontal scroll*/
  175.                  true);            /*vertical scroll*/
  176.     (**list).selFlags = lOnlyOne + lNoNilHilite;
  177.     return (list);
  178. } /*NewV1List*/
  179.  
  180. /*----------*/
  181. Boolean    GetListChoice    (short            *choice,
  182.                          ListHandle        list)
  183. {
  184.     Boolean            result;
  185.     Cell            selectedCell;
  186.  
  187.     SetPt (&selectedCell, 0, 0);
  188.     if (LGetSelect (true, &selectedCell, list)) {
  189.         *choice = selectedCell.v;
  190.         result = true;
  191.     } else {
  192.         *choice = -1;
  193.         result = false;
  194.     }
  195.  
  196.     return (result);
  197. } /*GetListChoice*/
  198.  
  199. /*----------*/
  200. void    SetListChoice    (short            choice,
  201.                          ListHandle        list)
  202. {
  203.     Cell            selectedCell;
  204.  
  205.     SetPt (&selectedCell, 0, choice);
  206.     LSetSelect (true, selectedCell, list);
  207. } /*SetListChoice*/
  208.  
  209. /*----------*/
  210. void    GetListRow        (Str255            data,
  211.                          short            index,
  212.                          ListHandle        list)
  213. {
  214.     Cell            selectedCell;
  215.     short            dataLen;
  216.  
  217.     SetPt (&selectedCell, 0, index);
  218.     dataLen = 255;        /*var parameter to LGetCell*/
  219.     LGetCell (&data [1], &dataLen, selectedCell, list);
  220.     data [0] = (char) dataLen;
  221. } /*GetListRow*/
  222.  
  223. /*----------*/
  224. void    SetListRow        (Str255            data,
  225.                          short            index,
  226.                          ListHandle        list)
  227. {
  228.     Cell            selectedCell;
  229.  
  230.     SetPt (&selectedCell, 0, index);
  231.     LSetCell (&data [1], data [0], selectedCell, list);
  232. } /*SetListRow*/
  233.  
  234. /*----------*/
  235. void AddToList        (Str255            data,
  236.                      ListHandle        list)
  237. {
  238.     short            newRow;
  239.     
  240.     #define maxint    32767
  241.  
  242.     newRow = LAddRow (1, maxint, list);
  243.     SetListRow (data, newRow, list);
  244. } /*AddToList*/
  245.  
  246. /*----------*/
  247. void DrawList    (ListHandle        list)
  248. {
  249.     PenState        savePen;
  250.     Rect            frame;
  251.  
  252.     GetPenState (&savePen);
  253.     PenNormal ();
  254.     frame = (**list).rView;
  255.     InsetRect (&frame, -1, -1);
  256.     FrameRect (&frame);
  257.     SetPenState (&savePen);
  258.     LUpdate ((**list).port->visRgn, list);
  259. } /*DrawList*/
  260.  
  261. /*----------*/
  262. void TextIDBox        (short            textID,
  263.                      Rect            bounds)
  264. {
  265.     Handle            text;
  266.  
  267.     text = GetResource ('TEXT', textID);
  268.     if (text != NULL) {
  269.         LoadResource (text);
  270.         HLock (text);
  271.         TextBox (&(**text), GetHandleSize (text), &bounds, textJust);
  272.         HUnlock (text);
  273.     }
  274. } /*TextIDBox*/
  275.  
  276. /*----------*/
  277. void PlotIconID        (short            iconID,
  278.                      Rect            bounds)
  279. {
  280.     Handle            icon;
  281.  
  282.     icon = GetIcon (iconID);
  283.     if (icon != NULL) {
  284.         LoadResource (icon);
  285.         PlotIcon (&bounds, icon);
  286.     }
  287. } /*PlotIconID*/
  288.  
  289. /*----------*/
  290. void DrawPictureID    (short            pictID,
  291.                      Rect            bounds)
  292. {
  293.     PicHandle        pict;
  294.  
  295.     pict = GetPicture (pictID);
  296.     if (pict != NULL) {
  297.         LoadResource ((Handle) (pict));
  298.         DrawPicture (pict, &bounds);
  299.     }
  300. } /*DrawPictureID*/
  301.  
  302. /*----------*/
  303. void DrawGrayLine    (Rect            bounds)
  304. {
  305.     PenState        savePen;
  306.  
  307.     GetPenState (&savePen);
  308.     PenNormal ();
  309. #ifdef dangerousPattern
  310.     PenPat (qd.gray);
  311. #else
  312.     PenPat (&qd.gray);
  313. #endif
  314.     MoveTo (bounds.left, bounds.top);
  315.     LineTo (bounds.right - 1, bounds.bottom - 1);
  316.     SetPenState (&savePen);
  317. } /*DrawGrayLine*/
  318.  
  319. /*----------*/
  320. static void DrawTriangle (const Rect     *bounds);
  321. static void DrawTriangle (const Rect     *bounds)
  322. {
  323.     PolyHandle        arrowTriangle;
  324.  
  325.     arrowTriangle = OpenPoly ();
  326.     MoveTo (bounds->right - 15, bounds->top + 5);
  327.     Line (5, 5);
  328.     Line (5, -5);
  329.     Line (-10, 0);
  330.     ClosePoly ();
  331. #ifdef dangerousPattern
  332.     FillPoly (arrowTriangle, qd.black);
  333. #else
  334.     FillPoly (arrowTriangle, &qd.black);
  335. #endif
  336.     KillPoly (arrowTriangle);
  337. } /*DrawTriangle*/
  338.  
  339. /*----------*/
  340. void UpdatePopup    (Rect            bounds,
  341.                      short            menuID,
  342.                      short            choice)
  343. {
  344.     MenuHandle        menu;
  345.     Str255            menuItem;
  346.     Rect            textRect;
  347.  
  348.     menu = (MenuHandle) (GetResource ('MENU', menuID));
  349.     if (menu != NULL) {
  350.         GetItem (menu, choice, menuItem);
  351.         textRect = bounds;
  352.         textRect.left = textRect.left + 12;        /*space for checkmark*/
  353.         textRect.right = textRect.right - 20;    /*space for triangle*/
  354.         TextBox (&menuItem [1], menuItem [0], &textRect, teJustLeft);
  355.     }
  356.  
  357.     DrawTriangle (&bounds);
  358.  
  359.     bounds.right  = bounds.right  - 1;
  360.     bounds.bottom = bounds.bottom - 1;
  361.     FrameRect (&bounds);
  362.     MoveTo (bounds.right, bounds.top + 2);
  363.     LineTo (bounds.right, bounds.bottom);
  364.     LineTo (bounds.left + 2, bounds.bottom);
  365. } /*UpdatePopup*/
  366.  
  367. /*----------*/
  368. void TrackPopup        (Rect            bounds,
  369.                      short            menuID,
  370.                      short            *choice)
  371. {
  372.     MenuHandle        menu;
  373.     Point            corner;
  374.     long            result;
  375.     Rect            triangleRect;
  376.  
  377.     menu = (MenuHandle) (GetResource ('MENU', menuID));
  378.     if (menu != NULL) {
  379.         InsertMenu (menu, -1);
  380.         triangleRect.top = bounds.top + 5;
  381.         triangleRect.left = bounds.right - 18;
  382.         triangleRect.bottom = triangleRect.top + 7;
  383.         triangleRect.right = triangleRect.left + 13;
  384.         EraseRect (&triangleRect);
  385.         corner = topLeft (bounds);
  386.         LocalToGlobal (&corner);
  387.         CheckItem (menu, *choice, true);
  388.         result = PopUpMenuSelect (menu, corner.v, corner.h + 1, *choice);
  389.         CheckItem (menu, *choice, false);
  390.         DeleteMenu (menuID);
  391.         InvalRect (&triangleRect);            /*because we always erase triangleRect*/
  392.         if ((HiWord (result) != 0) && (LoWord (result) != *choice)) {
  393.             *choice = LoWord (result);
  394.             InsetRect (&bounds, 1, 1);
  395.             InvalRect (&bounds);
  396.         }
  397.     }
  398. } /*TrackPopup*/
  399.  
  400. /*----------*/
  401. Boolean TrackButton        (ControlHandle    button,
  402.                          Point            mousePos)
  403. {
  404.     return (TrackControl (button, mousePos, NULL) != 0);
  405. } /*TrackButton*/
  406.  
  407. /*----------*/
  408. void TrackCheck        (ControlHandle    checkBox,
  409.                      Point            mousePos,
  410.                      Boolean        *checked)
  411. {
  412.     if (TrackControl (checkBox, mousePos, NULL) != 0) {
  413.         *checked = !*checked;
  414.         SetCtlValue (checkBox, *checked);
  415.     }
  416. } /*TrackCheck*/
  417.  
  418. /*----------*/
  419. void TrackRadio        (ControlHandle    radio,
  420.                      Point            mousePos,
  421.                      short            *choice)
  422. {
  423.     long            refCon;
  424.     short            group;
  425.     ControlHandle    control;
  426.  
  427.     if (TrackControl (radio, mousePos, NULL) != 0) {
  428.         refCon = GetCRefCon (radio);
  429.         group = HiWord (refCon);
  430.         *choice = LoWord (refCon);
  431.         control = ((WindowPeek) qd.thePort)->controlList;
  432.         while (control != NULL) {
  433.             if ((**control).contrlDefProc == (**radio).contrlDefProc) {
  434.                 refCon = GetCRefCon (control);
  435.                 if (HiWord (refCon) == group) {
  436.                     SetCtlValue (control, 0);
  437.                 }
  438.             }
  439.             control = (**control).nextControl;
  440.         } /*while*/
  441.         SetCtlValue (radio, 1);
  442.     }
  443. } /*TrackRadio*/
  444.  
  445. /*----------*/
  446. void TrackMulti        (ControlHandle    multi,
  447.                      Point            mousePos,
  448.                      short            *value)
  449. {
  450.     if (TrackControl (multi, mousePos, NULL) != 0) {
  451.         if (*value == GetCtlMax (multi)) {
  452.             *value = GetCtlMin (multi);
  453.         } else {
  454.             (*value)++;
  455.         }
  456.         SetCtlValue (multi, *value);
  457.     }
  458. } /*TrackMulti*/
  459.  
  460. /*----------*/
  461. void TrackPalette    (ControlHandle    palette,
  462.                      Point            mousePos,
  463.                      short            *choice)
  464. {
  465.     if (TrackControl (palette, mousePos, NULL) != 0) {
  466.         *choice = GetCtlValue (palette);
  467.     }
  468. } /*TrackPalette*/
  469.  
  470. /*----------*/
  471. void    EnableControl    (ControlHandle    theControl,
  472.                          Boolean        active)
  473. {
  474.     if (theControl != NULL) {
  475.         if (active) {
  476.             HiliteControl (theControl, 0);
  477.         } else {
  478.             HiliteControl (theControl, 255);
  479.         }
  480.     }
  481. } /*EnableControl*/
  482.  
  483. /*----------*/
  484. /* for backwards compatibility:*/
  485. /*----------*/
  486. void    HiliteScroll    (ControlHandle    scroll,
  487.                          Boolean        active)
  488. {
  489.     EnableControl (scroll, active);
  490. } /*HiliteScroll*/
  491.  
  492. /* WindowAids */
  493.